package com.zhuss.want.want;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.want.want.DetailWantBo;
import com.zhuss.bean.bo.want.want.SearchWantBo;
import com.zhuss.bean.bo.want.want.wechat.ListWantBo;
import com.zhuss.bean.bo.core.upload.DownloadBo;
import com.zhuss.bean.vo.want.want.DetailWantVo;
import com.zhuss.bean.vo.want.want.ListWantVo;
import com.zhuss.bean.vo.core.upload.DownloadVo;
import com.zhuss.want.processor.WantAppendRead;
import com.zhuss.want.processor.WantRead;
import com.zhuss.want.processor.WantTopicRead;
import com.zhuss.common.PageVo;
import com.zhuss.common.Processor;
import com.zhuss.common.ReadService;
import com.zhuss.enums.AppendTypeEnum;
import com.zhuss.exception.WpsException;


/**
 * want查询service（基本查询）
 * @author zhushanshuo
 * @Date 2020-02-05 15:59
 *
 */
@Component("wantReadService")
public class WantReadService implements ReadService<Object, Object> {
	
	@Autowired
	@Qualifier("wantRead")
	private WantRead wantRead;

	@Autowired
	@Qualifier("wantTopicRead")
	private WantTopicRead wantTopicRead;
	
	@Autowired
	@Qualifier("wantAppendRead")
	private WantAppendRead wantAppendRead;
	
	@Autowired
	@Qualifier("downloadProcessor")
	private Processor<DownloadVo, DownloadBo> downloadProcessor;
	
	
	
	/**
	 * 详情
	 */
	@Override
	public Object detail(Object param) throws WpsException {
		DetailWantBo bo = (DetailWantBo) param;
		Long wantId = bo.getWantId();
		DetailWantVo vo = (DetailWantVo) wantRead.detail(wantId);
		DownloadBo downloadBo = new DownloadBo();
		downloadBo.setKeyList(vo.getPic());
		DownloadVo downloadVo = downloadProcessor.process(downloadBo);
		vo.setUrlList(downloadVo.getUrlList());
		return vo;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	@Override
	public Object list(Object param) throws WpsException {
		SearchWantBo bo = (SearchWantBo) param;
		Object result = wantRead.list(bo);
		transfer(result, bo.getConcurrentUserId());
		return result;
	}
	
	/**
	 * 树
	 * @throws WpsException 
	 */
	public Object listTopicTree(DetailWantBo param) throws WpsException {
		Long wantId = param.getWantId();
		Object result = wantTopicRead.listTopicTree(wantId);
		return result;
	}
	
	/**
	 * 列表
	 * @throws WpsException 
	 */
	public Object listWant(Object param) throws WpsException {
		ListWantBo bo = (ListWantBo) param;
		Object result = wantRead.listWant(bo);
		transfer2(result, bo.getConcurrentUserId());
		return result;
	}
	
	@SuppressWarnings("unchecked")
	private void transfer2(Object result, Long userId) throws WpsException {
		PageVo<DetailWantVo> pageVo = (PageVo<DetailWantVo>) result;
		for (DetailWantVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = wantAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getWantId(), null).size();
			Integer collectNum = wantAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getWantId(), null).size();
			Integer forwardNum = wantAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getWantId(), null).size();
			Integer topicNum = wantTopicRead.listTopic(vo.getWantId(), 0l).size();
			
			Integer thumbSelfNum = wantAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getWantId(), userId).size();
			Integer collectSelfNum = wantAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getWantId(), userId).size();
			Integer forwardSelfNum = wantAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getWantId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
		}
	}
	
	@SuppressWarnings("unchecked")
	private void transfer(Object result, Long userId) throws WpsException {
		PageVo<ListWantVo> pageVo = (PageVo<ListWantVo>) result;
		for (ListWantVo vo : pageVo.getItems()) {
			DownloadBo downloadBo = new DownloadBo();
			downloadBo.setKeyList(vo.getPic());
			DownloadVo downloadVo = downloadProcessor.process(downloadBo);
			vo.setUrlList(downloadVo.getUrlList());
			
			Integer thumbNum = wantAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getWantId(), null).size();
			Integer collectNum = wantAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getWantId(), null).size();
			Integer forwardNum = wantAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getWantId(), null).size();
			Integer topicNum = wantTopicRead.listTopic(vo.getWantId(), 0l).size();
			
			Integer thumbSelfNum = wantAppendRead.listByType(AppendTypeEnum.点赞.key(), vo.getWantId(), userId).size();
			Integer collectSelfNum = wantAppendRead.listByType(AppendTypeEnum.收藏.key(), vo.getWantId(), userId).size();
			Integer forwardSelfNum = wantAppendRead.listByType(AppendTypeEnum.转发.key(), vo.getWantId(), userId).size();
			
			vo.setThumbNum(thumbNum);
			vo.setCollectNum(collectNum);
			vo.setForwardNum(forwardNum);
			vo.setTopicNum(topicNum);
			
			vo.setThumbSelfNum(thumbSelfNum);
			vo.setCollectSelfNum(collectSelfNum);
			vo.setForwardSelfNum(forwardSelfNum);
			
		}
	}
	
	
}

